import pymysql
import os
import json
import re
import requests
from lxml import etree
from bs4 import BeautifulSoup as soup
from time import sleep
from datetime import datetime
class nowArt:
    def __init__(self):
        self.host = "192.168.0.113"
        self.port = 3306
        self.user='coname'
        self.password = 'xZS6p4LDcKCwn4Yb'
        self.db="co"
        self.charset="utf8"
        self.num=1
        self.urlBase='http://www.dangdaiyishu.com'
        #/user/4075/upload/12-02-02/1328166921_afile.jpg
        #/cache/img/4075/12-02-02/1328166921_afile.jpg
        #http://www.dangdaiyishu.com/user/4075/upload/12-02-02/1328166921_afile.jpg  
        self.urlLeft = 'http://www.dangdaiyishu.com/index.php?page='
        self.urlRight = '&act=app&appid=38&tag='    

        self.typeto = '%E5%9B%BD%E7%94%BB' #  %E5%9B%BD%E7%94%BB  %E6%B2%B9%E7%94%BB  %E7%89%88%E7%94%BB %E9%9B%95%E5%A1%91 %E6%B0%B4%E7%B2%89 %E6%B0%B4%E5%BD%A9  %E7%B4%A0%E6%8F%8F
        #30 国画   16油画   24版画  7雕塑 4水粉   9水彩 18素描  
        self.maxNum = 30
        self.ty = '国画'

        self.header={"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;"}
        self.nu=1

    def open_mysql(self):#打开数据库
        try:
            conn = pymysql.connect(host=self.host,port=self.port,user=self.user,passwd=self.password,db=self.db,charset=self.charset)
            cursor =  conn.cursor()
            print('连接数据库成功!')
            return conn,cursor     
        except Exception as e:
            print("连接数据库失败>>>",e)

    def close_mysql(self,conn,cursor):#关闭数据库
        try:
            cursor.close()
            conn.close()
            print("关闭数据成功")
        except Exception as e:
            print("关闭数据库失败>>>",e)

    def input_mysql(self,datas):#存入数据库
        conn,cursor = self.open_mysql()
        try:
            if not cursor.execute('select fileurl from co_art_product where fileurl ="{}"'.format(datas[4])):
                cursor.execute('insert into co_art_product (keywordid,srname,srtype,srtypeto,fileurl,srimg) values("{}","{}","{}","{}","{}","{}")'.format(datas[0],datas[1],datas[2],datas[3],datas[4],datas[5]))
                conn.commit()
                print('插入数据成功!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
        except Exception as e:
            print("插入数据失败$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$>>>",e)
        self.close_mysql(conn,cursor)

    def spide_title(self):#爬取网页数据
        print(self.num)
        try:
            content = requests.get(self.urlLeft+str(self.num)+self.urlRight+self.typeto,verify=False,headers=self.header).text
        # print(content)
        except Exception as e:
            print('连接失败>>>',e)
        pat= '<img src="(.*?)" onload='
        pat2 =  '<li class="n39y n_old"><a href="(.*?)" target="_blank">(.*?)</a></li>'
        datas = re.findall(pat,content)
        datas2 = re.findall(pat2,content)
        numE=len(datas2)
        tamp = "{0:%y%m%d%H%M%S}".format(datetime.now())
        tamp = int(tamp)
        for numi in range(0,numE,1):
            tamp=tamp+self.nu
            results=[]
            results.append(tamp)
            results.append(datas2[numi][1])
            results.append('nowArt')
            results.append(self.ty)
            res2 = self.urlBase + datas2[numi][0]
            results.append(res2)
            res1 = self.urlBase + datas[numi]
            results.append(res1)
            # print(results)
            self.input_mysql(results)
            self.nu = self.nu+1
        #翻页
        
        if self.num<self.maxNum+1:
            self.num=self.num+1
        else:
            return
        self.spide_title()

    def run(self):
        self.spide_title()
        
if __name__ == "__main__":
    nowArt().run()